package com.example.meituan.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;

import com.example.meituan.activity.bean.TypeBean;
import com.example.meituan.db.DBUntil;

public class TypeDao {

    private static SQLiteDatabase db=DBUntil.con;
    // 添加类型
    public static int addType(TypeBean type) {
        ContentValues values = new ContentValues();
        values.put("s_id", type.getId());
        values.put("s_shop_id", type.getShopId());
        values.put("s_type_des", type.getTypeDescription());
        try{
            db.insert("d_type", null, values);
            return 1;
        }catch (Exception e){
            return 1;
        }

    }

    // 删除类型
    public static int deleteTypeById(String id) {
        return db.delete("d_type", "s_id=?", new String[]{id});
    }

    // 更新类型
    public static int updateType(TypeBean type) {
        ContentValues values = new ContentValues();
        values.put("s_shop_id", type.getShopId());
        values.put("s_type_des", type.getTypeDescription());

        return db.update("d_type", values, "s_id=?", new String[]{type.getId()});
    }

    // 查询所有类型
    public List<TypeBean> getAllTypes() {
        List<TypeBean> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from d_type", null);

        try {
            while (cursor.moveToNext()) {
                TypeBean type = createTypeFromCursor(cursor);
                list.add(type);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return list;
    }

    public static List<TypeBean> getAllTypesByShopId(String shopId) {
        List<TypeBean> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from d_type where s_shop_id=?", new String[]{shopId});

        try {
            while (cursor.moveToNext()) {
                TypeBean type = createTypeFromCursor(cursor);
                list.add(type);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return list;
    }

    // 根据 ID 查询单个类型
    public static TypeBean getTypeById(String id) {
        Cursor cursor = db.rawQuery("select * from d_type where s_id=?", new String[]{id});

        try {
            if (cursor.moveToNext()) {
                return createTypeFromCursor(cursor);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return null;
    }

    // 辅助方法：从游标创建类型对象
    private static TypeBean createTypeFromCursor(Cursor cursor) {
        TypeBean type = new TypeBean(
                cursor.getString(cursor.getColumnIndexOrThrow("s_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_shop_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_type_des"))
        );
        return type;
    }
}
